$(function(){
    //列表异步点击保存
    $('.ajax_action').click(function(){
        var data = $(this).parents('tr').data() || {},
            data2 = $(this).data() || {},
            tip = $(this).attr('tip') || "操作",
            url = $(this).attr('url') || "";
        $.extend(data, data2);
        swal({
            title: "您确定要"+tip+"吗？",text: "该操作很重要，请谨慎操作！",confirmButtonText: "确定",type: "warning",showCancelButton: true,confirmButtonColor: "#DD6B55",cancelButtonText: "让我再考虑一下…",closeOnConfirm: false
        }, function (isConfirm) {
            if (isConfirm) {
                $.ajax({
                    type: "post",
                    url: url,
                    data: data,
                    dataType: "json",
                    success: function(param){
                        if(param.code == '20000'){
                            swal(tip+"成功！", param.message, "success");
                            setTimeout(function(){
                                location.reload();
                            },1500);
                        }else{
                            swal(tip+"失败！", param.message != undefined ? param.message : "此次操作失败", "error")
                        }
                    }
                });
            }
        });
    });

    //驳回审核
    $('.reject_ajax_action').click(function(){
        var data = $(this).parents('tr').data() || {},
            data2 = $(this).data() || {},
            url = $(this).attr('url'),
            html = '';
        if(url == 'video-checked-ajax-do'){
            //视频
            html =  "<label><input type='checkbox' name='reject_radio'> <small>请添加与内容相关的视频封面</small></label><br/>" +
                    "<label><input type='checkbox' name='reject_radio'> <small>请完善视频介绍</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_radio'> <small>清晰度过低，上传视频画质要求在720P以上</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_radio'> <small>音质过低，上传视频要求不含杂音、音量适中、清晰流畅、普通话标准、语速适中</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_radio'> <small>内容不得有推广类信息、不良信息等与视频不相关内容</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_radio'> <small>录制环境不符合标准</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_radio'> <small>请添加与视频内容相关的视频封面</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_radio'> <small>请丰富视频介绍</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_radio'> <small>视频介绍内不得出现机构名称及相关推广信息</small></label><br/>";
            html = "<div class='reject_box'>"+html+"<input type='text' value='' name='reject_content' placeholder='填写自定义理由'/></div>";
        }else if(url == '/teacher/checked-ajax-do'){
            //教师
            html =  "<label><input type='checkbox' name='reject_content'> <small>身份证信息与所填信息不符</small></label><br/>" +
                    "<label><input type='checkbox' name='reject_content'> <small>身份证照片正面、反面模糊</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>手持身份证信息面模糊</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>上传证件不符合要求</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>教师资格证与所填信息不符</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>教师资格证与身份证信息不符</small></label><br/>";
            html = "<div class='reject_box'><h3>驳回理由</h3>"+html+"<h3>备注</h3><textarea name='notes_content' placeholder='请填写备注信息' style='width: 100%; height: 80px;'></textarea></div>";
        }else if(url == '/teacher/authen-teacher-info-ajax-do'){
            //教师资料
            html =  "<label><input type='checkbox' name='reject_content'> <small>头像图片含广告信息</small></label><br/>" +
                    "<label><input type='checkbox' name='reject_content'> <small>头像图片含不良语言</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>头像图片引起不适（色情、血腥等）</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>老师简介含网站、QQ号、微信号</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>老师简介包含广告信息</small></label><br/>";
            html = "<div class='reject_box'><h3>驳回理由</h3>"+html+"</div>";
        }else if(url == '/organization/checked-ajax-do'){
            //机构
            html =  "<label><input type='checkbox' name='reject_content'> <small>头像图片含广告信息</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>头像图片含不良语言</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>头像图片引起不适（色情、血腥等）</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>机构简介含网站、QQ号、微信号</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>机构简介包含广告信息</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_content'> <small>信用代码与营业执照的代码不</small></label><br/>";
            html =  "<div class='reject_box'><h3>驳回理由</h3>"+html+"<h3>备注</h3><textarea name='notes_content' placeholder='请填写备注信息' style='width: 100%; height: 80px;'></textarea></div>";
        }else{
            //课程
            html =  "<label><input type='checkbox' name='reject_radio'> <small>请添加与内容相关的课程封面</small></label><br/>" +
                    "<label><input type='checkbox' name='reject_radio'> <small>请完善课程介绍</small></label><br/>"+
                    "<label><input type='checkbox' name='reject_radio'> <small>课程介绍内不得出现机构名称及相关推广信息</small></label><br/>";
            html = "<div class='reject_box'>"+html+"<input type='text' value='' name='reject_content' placeholder='填写自定义理由'/></div>";
        }
        $.extend(data, data2);
        swal({
          title: "请填写驳回理由",
          text: html,
          showCancelButton:true,
          html: true
        },
        function(){
            if(url == '/teacher/checked-ajax-do' || url == '/teacher/authen-teacher-info-ajax-do' || url == '/organization/checked-ajax-do'){
                data.reject_content = '';
                $.each($('.reject_box input[name=reject_content]:checked'),function(i,n){
                    data.reject_content += $(n).next('small').text()+'；';
                })
                data.notes_content = $('.reject_box textarea[name=notes_content]').val();
            }else{
                data.reject_content = $('.reject_box input[name=reject_content]').val();
                if(data.reject_content !== ''){
                    data.reject_content += '；';
                }
                $.each($('.reject_box input[name=reject_radio]:checked'),function(i,n){
                    data.reject_content += $(n).next('small').text()+'；';
                })
            }
            $.ajax({
                type: "post",
                url: url,
                data: data,
                dataType: "json",
                success: function(param){
                    if(param.code == '20000'){
                        swal("操作成功！", param.message, "success");
                        setTimeout(function(){
                            location.reload();
                        },1500);
                    }else{
                        swal("操作失败！", param.message != undefined ? param.message : "此次操作失败", "error")
                    }
                }
            });
        });
    });

    //弹出框异步点击保存
    $('.ajax_modal_action .save_btn').click(function(){
        var modal = $(this).parents('.ajax_modal_action'),
            data = modal.find('form').serialize(),
            url = modal.find('form').attr('data-url');
        $.ajax({
            type: "post",
            url: url,
            data: data,
            dataType: "json",
            success: function(param){
                if(param.code == '20000'){
                    swal("操作成功！", param.message, "success");
                    setTimeout(function(){
                        location.reload();
                    },1500);
                }else{
                    swal("操作失败！", "此次操作失败", "error")
                }
            }
        });
    });

    //全局列表排序修改
    $('input.edit_sort').change(function(){
        var _this = $(this);
        var data = $(this).data();
        data.sort = $(this).val();
        if(!data.sort.match(/^\d+$/)){
            swal("排序修改失败！", "请输入大于0的整数值", "error");
            return false;
        }
        if(data.model == undefined || data.model == ''){
            swal("排序修改失败！", "参数错误", "error");
            return false;
        }
        if(data.id == undefined || data.id == ''){
            swal("排序修改失败！", "参数错误", "error");
            return false;
        }
        $.post('/site/sort-edit',data,function(data){
            if(data.code == 20000){
                _this.css({'border-color':'#1ab394'});
            }else{
                swal("排序修改失败！", data.message, "error");
                _this.css({'border-color':'#ed5565'});
            }
        },'json');
    });

    //改变地区选择框
    $('body').on('change','.select_area select',function(){
        var type = $(this).attr('class'),
            id = $(this).val();
        $(this).nextAll('select').find('option:gt(0)').remove();
        if(type == 'country' || id === '') return ;
        get_area(id,$(this).next('select'));
    });
    //新增一行区域
    $('.add_area_box .add_area').click(function(){
        prov_select_i++;
        $('.add_area_box').before('<div class="form-group">\
                <label class="col-sm-2 control-label"></label>\
                <div class="col-sm-5 select_area form-inline sign">\
                <select name="area['+prov_select_i+'][province]" class="province form-control"><option value="">选择省份</option></select>\
                <select name="area['+prov_select_i+'][city]" class="city form-control"><option value="">选择城市</option></select>\
                <select name="area['+prov_select_i+'][country]" class="country form-control"><option value="">选择地区</option></select>\
                <button type="button" class="btn btn-info btn-xs del_area">删除</button>\
                </div>\
             </div>');
        get_area(0,$('.select_area:last .province'));
    });
    //删除一行区域
    $('body').on('click','.select_area .del_area',function(){
        $(this).parents('.form-group').remove();
    });
});


//根据id获取下级区域，并填充select
var prov_select_html = '';
var prov_select_i = 10000;
function get_area(id,obj){
    if(id == 0 && prov_select_html != ''){
        obj.append(prov_select_html);
        return true;
    }
    var sign = 0;
    if(obj.parent('.select_area').hasClass('sign')){
        sign = 1;
    }
    $.post('/site/getarea',{'id':id,'sign':sign},function(res){
        if(res.code == 20000 && res.data != undefined){
            var html = '';
            $.each(res.data,function(i,n){
                var is_disabled = sign == 1 && n.sign != undefined && n.sign == 1;
                html+='<option value="'+ n.id+'" '+(is_disabled ? 'disabled':'')+' >'+ n.name+(is_disabled ? '(已签约)':'')+'</option>';
            });
            if(id == 0){
                prov_select_html = html;
            }
            obj.append(html);
        }
    },'json');
}

function get_course_class(id,index){
    $.post('/course/get-child-list',{'class_id':id},function(res){
        if(res.code == 20000 && res.data != undefined){
            var html = '';
            $.each(res.data,function(i,n){
                html+='<option value="'+ n.id+'">'+ n.name+'</option>';
            });
            $('select[name="class['+(index+1)+']"]').append(html);
        }
    },'json');
}

//oss 图片转换
function image_resize(data){
    if(data.url == undefined){
        return '';
    }
    if(data.width != undefined && data.high != undefined){
        return data.url + '?x-oss-process=image/resize,m_fixed,h_'+data.high+',w_'+data.width;
    }
    if(data.width != undefined){
        return data.url + '?x-oss-process=image/resize,w_'+data.width;
    }
    if(data.high != undefined){
        return data.url + '?x-oss-process=image/resize,h_'+data.high;
    }
    return data.url;
}